﻿

namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using System.Web.Mvc;
    using Mulala.Cavan.Core.Extension;

    public class AreaController : EntityController<Area, string, AreaSearch, IAreaViewModel, IAreaAppService>, IAreaController
    {
        public override string ControllerName
        {
            get { return ControllerConst.AREA; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public AreaController()
        {
            this.Model = ModelFactory.CreateModel<IAreaViewModel>();
        }

        [ActionName("selectarea")]
        public ActionResult GetArea(string cityID, string townID)
        {
            IAreaViewModel areaViewModel = ModelFactory.CreateModel<IAreaViewModel>();
            AreaSearch areaSearch = AreaSearchFactory.CreateAreaSearch(2);
            IList<Area> cities = this.AppService.GetBySearch(areaSearch).ToList();
            List<SelectListItem> cityItems = new List<SelectListItem>();
            cityItems.Add(new SelectListItem { Text = "请选择", Value = "0", Selected = cityID.IsNotEmpty() ? false : true });
            foreach (Area a in cities)
            {
                cityItems.Add(new SelectListItem { Text = a.Name, Value = a.ID, Selected = a.ID == cityID ? true : false });
            }
            areaViewModel.Cities = cityItems;

            SetTownItem(areaViewModel, cityID);
            if (townID.IsNotEmpty() && townID.Length == 6)
            {
                Area town = this.AppService.GetById(townID);

                areaViewModel.Towns.FirstOrDefault(t => t.Value == town.ID).Selected = true;
            }
            else
            {
                areaViewModel.Towns.FirstOrDefault().Selected = true;
                return PartialView(areaViewModel);
            }

            return PartialView(areaViewModel);
        }

        [ActionName("selecttown")]
        public JsonResult GetTown(string id)
        {
            IAreaViewModel areaViewModel = ModelFactory.CreateModel<IAreaViewModel>();
            SetTownItem(areaViewModel, id);

            return Json(areaViewModel);
        }

        private void SetTownItem(IAreaViewModel areaViewModel, string parentID)
        {
            AreaSearch areaSearch = AreaSearchFactory.CreateAreaSearch(parentID, true);
            IList<Area> towns = this.AppService.GetBySearch(areaSearch).ToList();
            List<SelectListItem> townItems = new List<SelectListItem>();
            townItems.Add(new SelectListItem { Text = "请选择", Value = "0", Selected = false });
            foreach (Area a in towns)
            {
                townItems.Add(new SelectListItem { Text = a.Name, Value = a.ID });
            }
            areaViewModel.Towns = townItems;
        }
    }
}
